var $table;
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    $table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    var currentActivityId = json.currentActivityId;
    var activityProductStatusList = json.activityProductStatusList;
    var detail = json.detail;
    var actProducts = null;
    if (detail) {
        actProducts = detail.actProducts;
    }
    var data = {"code": 0, "msg": "", "count": json.totalCount, "items": actProducts};

    function getStatusStr(status) {
        var statusStr = '-';
        $.each(activityProductStatusList, function (index, item) {
            if (status == item.first) {
                statusStr = item.second;
            }
        });
        return statusStr;
    }

    var cols = [
        [
            {field: 'activityProductId', type: 'checkbox', fixed: 'left', width: '50', align: 'center'},
            {field: 'productId', title: 'ID', width: '50', align: 'center', unresize: true, sort: false},
            {field: 'servCategName', title: '服务分类', width: '250', align: 'center', unresize: true, sort: false},
            {field: 'categOneName', title: '一级分类', width: '250', align: 'center', unresize: true, sort: false},
            {field: 'categTwoName', title: '二级分类', width: '250', align: 'center'},
            {field: 'productName', title: '产品名称', tips: 'productName', width: '250', align: 'center'},
            {
                field: 'status', title: '状态', width: '100', align: 'center', unresize: true, templet: function (d) {
                    return getStatusStr(d.status);
                }
            }
        ]
    ];

    let ids = new Array();
    // 选择（全选或单选）
    table.on("checkbox(zmn-act-modify-product-table)", function (obj) {
        ids = table.checkStatus('zmn-act-modify-product-table').data.map((item) => item.activityProductId);
    });

    // 上架 下架
    table.on("toolbar(zmn-act-modify-product-table)", function (obj) {
        if (obj.event != "act-product-putaway" && obj.event != "act-product-sold-out") {
            return;
        }
        if (ids.length === 0) {
            return layer.msg("请选择至少1个产品！", {icon: 5, shift: 6});
        }
        let data = {activityId: currentActivityId, activityProductIds: ids};
        switch (obj.event) {
            case 'act-product-putaway':
                var url = '/activity/config/act-product-putaway.action';
                let putaway_index = layer.confirm('确定上架吗？', function () {
                    $.ajax({
                        async: false,
                        type: "POST",
                        url: url,
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: function (json) {
                            layer.close(putaway_index);
                            addProductEndCallback();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        }
                    });

                });
                break;
            case 'act-product-sold-out':
                var url = '/activity/config/act-product-sold-out.action';
                let sold_out_index = layer.confirm('确定下架吗？', function () {
                    //zmnPage.ajaxSimple(url, {activityId: currentActivityId, ids: ids}, 1000);
                    $.ajax({
                        async: false,
                        type: "POST",
                        url: url,
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: function (json) {
                            layer.close(sold_out_index);
                            addProductEndCallback();
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        }
                    });

                });
                break;
        }
    });

    form.on('submit(modify-product-query-submit)', function (obj) {
        var query = obj.field;
        reloadTable({'activityId': currentActivityId, 'productName': query.productName});
        return false;
    });
    let tableRender = function (data) {
        table.render({
            elem: "#zmn-act-modify-product-table",
            toolbar: "#zmn-act-modify-product-table-tool",
            data: data,
            cols: cols,
            // title: "活动配置管理",
            limit: json.pageSize,
            page: true,
            height: 'full-120',
            defaultToolbar: ['filter']
        });
    };

    tableRender(actProducts);

    var addProductEndCallback = function () {
        $('#modify-product-query-submit').click();
    };

    var reloadTable = function (data) {
        var actionUrl = '/activity/config/act-product-query';
        // zmnLayUtil.layTableRender(table,cols,"zmn-act-modify-product-table",data,actionUrl, data);
        $.ajax({
            async: false,
            type: "POST",
            url: actionUrl,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (res) {
                if (res.status == 200) {
                    actProducts = res.items;
                    tableRender(actProducts);
                    return false;
                } else {
                    layer.msg(res.message);
                }
            }
        });
    };

    $('body').on('click', '#btn-skip-product-page', function () {
        var url = '/activity/config/product-query.action?activityId=' + currentActivityId;
        let data = {
            title: '',
            shade: 0.8,
            maxmin: false,
            resize: false,
            closeBtn: 1,
            end: addProductEndCallback
        };
        zmnLayUtil.openIframeCustom(layer, url, data);
    });

    //对外暴露的接口
    exports('zmn_plat_activity_modifyProduct', {});
});





